<template>
  <CompOperate
    :field="field"
    :designer="designer"
    :design-state="designState"
    :parent-list="parentList"
    :parent-widget="parentWidget"
    :sub-form-row-id="subFormRowId"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :index-of-parent-list="indexOfParentList"
    :display-style="field['options'].displayStyle"
  >
    <div class="scene-head-widget" :style="getStyle('head')">
      <div class="wooke-and-date-Box">
        <div class="week">{{ getWeekInfo(week) }}</div>
        <div class="date">{{ date }}</div>
      </div>
      <div class="content-Box">
        {{ getRoomDeptInfo[optionsInfo["deptNameKey"]] || "\u00a0" }}
      </div>
      <div class="timer-Box"  :style="getStyle('timerBackGroundColor')">
        <div>{{ time }}</div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import dayjs from "dayjs";
import { NoticeBar } from "vant";
import emitter from "@/utils/designer/emitter";
import { getWeek, pxToVw } from "@/utils/common";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import { buildParentApi } from "@/utils/designer/util";

export default {
  name: "scene-head-Content-center-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    CompOperate,
    NoticeBar,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi(),
    },
  },

  data() {
    return {
      week: "",
      optionsInfo: Object(),
      mockOpen: false,
      date: dayjs().format("YYYY年MM月DD日"),
      time: dayjs().format("HH:mm"),
    };
  },

  computed: {
    timeType() {
      return this.optionsInfo.timeValue;
    },

    // 获取日期
    getWeekInfo() {
      return function (info) {
        return getWeek(info);
      };
    },

    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = self.optionsInfo || Array();
        let Obj = Object();
        switch (val) {
          case "head":
            Reflect.set(Obj, "height", pxToVw(info.headHeight));
            Reflect.set(Obj, "backgroundColor", `${info.backgroundColor}`);
            Reflect.set(Obj, "color", `${info.fontColor}`);
            return Obj;
          case 'timerBackGroundColor':
            Reflect.set(Obj, "backgroundColor", `${info.dataHoursMinBack}`);
           return Obj;
        }
      };
    },

    //获取LOGO资源
    pictureResource() {
      let { imageUrl } = this.optionsInfo;
      if (this.$lodash.isObject(imageUrl)) {
        return imageUrl?.url;
      } else {
        return imageUrl;
      }
    },

    // 获取科室信息
    getRoomDeptInfo() {
      let devType = this.devType;
      let roomDoctorInfo = this.$store.getters["getRoomDeptInfo"];
      if (devType) {
        return roomDoctorInfo.apiData;
      } else {
        return roomDoctorInfo.mockData;
      }
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.updateTime();
    this.handleOnMounted();
  },

  methods: {
    // 获取时间
    updateTime(val) {
      let timeType = this.optionsInfo.timeValue;
      if (!!val) {
        let msg = JSON.parse(val);
        if (!!msg && !!msg.timestamp) {
          let timestamp = dayjs.unix(msg.timestamp);
          [this.week, this.date, this.time] = dayjs(timestamp)
            .format(`d,${timeType},HH:mm`)
            .split(",");
        } else {
          [this.week, this.date, this.time] = dayjs()
            .format(`d,${timeType},HH:mm`)
            .split(",");
        }
      } else {
        [this.week, this.date, this.time] = dayjs()
          .format(`d,${timeType},HH:mm`)
          .split(",");
      }
    },

    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "headerInfo":
          this.updateTime(data);
          break;
      }
    },
  },

  destroyed() {
    this.unregisterFromRefList();
  },

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        let key = val?.options?.config;
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true,
    },

    // 监听时间更新
    timeType: {
      handler: function (val) {
        if (val) {
          this.updateTime();
        }
      },
      deep: true,
      immediate: true,
    },
  },
};
</script>

<style lang="scss" scoped>
.scene-head-widget {
  padding: 28px 30px;
  box-sizing: border-box;
  background-color: transparent;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #333;
  height: 150px;
  overflow: hidden;

  .wooke-and-date-Box {
    font-weight: 500;
    font-size: 40px;
    color: #ffffff;
    .week {
      text-align: center;
      line-height: 1.5;
    }
  }
  .content-Box {
    font-weight: 500;
    font-size: 90px;
    color: #ffffff;
  }
  .timer-Box {
    font-weight: 500;
    font-size: 84px;
    color: #ffffff;
    background: #FF9A5A;
    border-radius: 14px;
    padding: 10px 20px ;
  }
}
</style>
